<?php
namespace app\controllers;


use app\common\corelib\LocalUpload;
use app\common\PayMethodEnum;
use app\logic\ChannelLogic;
use app\logic\OrderLogic;
use app\models\Address;
use PHPExcel_Cell_DataType;
use yii;

class OrderController extends BackendController
{

    public function actionIndex()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $orderLogic = new OrderLogic();
        $this->data['breadcrumbs'] = '订单管理';
        $channelLogic = new ChannelLogic();
        //var_dump($orderLogic->getOrder($query, $page));exit;
        $payMethodEnum = new PayMethodEnum();
        $pay_type = get_object_vars($payMethodEnum);
        $order = $orderLogic->getOrder($query, $page);
        if(Yii::$app->request->get('submit')!="export" && Yii::$app->request->get('submit')!="export-goods"){
            return $this->render('index', [
                'theme' => '/assets/',
                'channel'=>$channelLogic->getChannels(),
                'pay_type'=>$pay_type,
                'order' => $order,
                'query' => $query
            ]);
        }elseif(Yii::$app->request->get('submit')=="export-goods"){
            $order = $orderLogic->getOrderGoods($query,1,null);
            //var_dump($order);exit;
            $objPHPExcel = new \PHPExcel();
            $objPHPExcel->getProperties()->setCreator("中经汇通")
                ->setTitle("商品销售明细列表");
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '商品ID')
                ->setCellValue('B1', '订单号')
                ->setCellValue('C1', '下单时间')
                ->setCellValue('D1', '付款时间')
                ->setCellValue('E1', '完成时间')
                ->setCellValue('F1', '订单状态')
                ->setCellValue('G1', '支付方式')
                ->setCellValue('H1', '销售数量')
                ->setCellValue('I1', '合约单价')
                ->setCellValue('J1', '销售单价')
                ->setCellValue('K1', '合约产品应付合计')
                ->setCellValue('L1', '销售产品应付合计')
                ->setCellValue('M1', '订单实付金额')
                ->setCellValue('N1', '支付流水号');

            $over_time = "";
            $status = "";
            foreach($order['lists'] AS $ke=>$va){
                switch($va['order_status']){
                    case 0:
                        $status = "待支付";
                        break;
                    case 1:
                        $status = "已付款";
                        break;
                    case 2:
                        $status = "已取消";
                        break;
                    case 3:
                        $status = "已发货";
                        break;
                    case 4:
                        $over_time = $va['update_time'];
                        $status = "已完成";
                        break;
                    case 5:
                        $status = "待审核";
                        break;
                    case 6:
                        $status = "代支付订单";
                        break;
                    case 7:
                        $status = "中经已代付";
                        break;
                }
                $keys = $ke+2;
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValueExplicit('A'.$keys, $va['yx_goods_id'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValueExplicit('B'.$keys, $va['order_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('C'.$keys, $va['order_time'])
                    ->setCellValue('D'.$keys, $va['pay_time'])
                    ->setCellValue('E'.$keys, $over_time)
                    ->setCellValue('F'.$keys, $status)
                    ->setCellValue('G'.$keys, $va['pay_type'])
                    ->setCellValue('H'.$keys, $va['buy_num'])
                    ->setCellValue('I'.$keys, $va['channel_price'])
                    ->setCellValue('J'.$keys, $va['sku_price'])
                    ->setCellValue('K'.$keys, $va['buy_num']*$va['channel_price'])
                    ->setCellValue('L'.$keys, $va['buy_num']*$va['sku_price'])
                    ->setCellValue('M'.$keys, $va['order_amount'])
                    ->setCellValueExplicit('N'.$keys, $va['pay_no'],PHPExcel_Cell_DataType::TYPE_STRING);


            }
            $objPHPExcel->setActiveSheetIndex(0);

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            header('pragma:public');
            header("Content-Disposition:attachment;filename=商品销售明细列表".date('_YmdHis').".xlsx");
            $objWriter->save('php://output');exit;
        }else{
            $order = $orderLogic->getOrder($query,1,null);
            //var_dump($order);exit;
            $objPHPExcel = new \PHPExcel();
            $objPHPExcel->getProperties()->setCreator("中经汇通")
                ->setTitle("订单列表");
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '订单号')
                ->setCellValue('B1', '手机号')
                ->setCellValue('C1', '商品数量')
                ->setCellValue('D1', '包裹数量')
                ->setCellValue('E1', '合约合计')
                ->setCellValue('F1', '销售合计')
                ->setCellValue('G1', '运费')
                ->setCellValue('H1', '应付金额')
                ->setCellValue('I1', '实付金额')
                ->setCellValue('J1', '订单状态')
                ->setCellValue('K1', '支付方式')
                ->setCellValue('L1', '支付流水号')
                ->setCellValue('M1', '渠道来源')
                ->setCellValue('N1', '下单时间')
                ->setCellValue('O1', '付款时间')
                ->setCellValue('P1', '完成时间')
                ->setCellValue('Q1', '收件人')
                ->setCellValue('R1', '收件人电话')
                ->setCellValue('S1', '收件人地址')
                ->setCellValue('T1', '物流单号')
                ->setCellValue('U1', '物流状态');

            $over_time = "";
            $status = "";
            foreach($order['lists'] AS $ke=>$va){
                switch($va['order_status']){
                    case 0:
                        $status = "待支付";
                        break;
                    case 1:
                        $status = "已付款";
                        break;
                    case 2:
                        $status = "已取消";
                        break;
                    case 3:
                        $status = "已发货";
                        break;
                    case 4:
                        $over_time = $va['update_time'];
                        $status = "已完成";
                        break;
                    case 5:
                        $status = "待审核";
                        break;
                    case 6:
                        $status = "代支付订单";
                        break;
                    case 7:
                        $status = "中经已代付";
                        break;
                }
                $package_status = [];
                $package_status_arr = explode(',',$va['package_status']);
                foreach($package_status_arr AS $kee=>$vaa){
                    switch($vaa){
                        case 0:
                            $package_status[] = "已申请售后";
                            break;
                        case 1:
                            $package_status[] = "待支付";
                            break;
                        case 2:
                            $package_status[] = "待发货";
                            break;
                        case 3:
                            $package_status[] = "已取消";
                            break;
                        case 4:
                            $package_status[] = "已发货";
                            break;
                        case 5:
                            $package_status[] = "已完成";
                            break;
                        case 6:
                            $package_status[] = "已取消售后";
                            break;
                        case 7:
                            $package_status[] = "已完成售后";
                            break;
                    }
                }
                $keys = $ke+2;
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValueExplicit('A'.$keys, $va['order_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValueExplicit('B'.$keys, $va['mobile'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('C'.$keys, $va['goods_num'])
                    ->setCellValue('D'.$keys, $va['package_num'])
                    ->setCellValue('E'.$keys, $va['channel_amount'])
                    ->setCellValue('F'.$keys, $va['goods_amount'])
                    ->setCellValue('G'.$keys, $va['shipping_fee'])
                    ->setCellValue('H'.$keys, $va['order_amount'])
                    ->setCellValue('I'.$keys, $va['order_amount'])
                    ->setCellValue('J'.$keys, $status)
                    ->setCellValue('K'.$keys, $va['pay_type'])
                    ->setCellValueExplicit('L'.$keys, $va['pay_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('M'.$keys, $va['channel_name'])
                    ->setCellValue('N'.$keys, $va['order_time'])
                    ->setCellValue('O'.$keys, $va['pay_time'])
                    ->setCellValue('P'.$keys, $over_time)
                    ->setCellValue('Q'.$keys, $va['consignee'])
                    ->setCellValueExplicit('R'.$keys, $va['consignee_phone'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('S'.$keys, $va['consignee_province'].$va['consignee_city'].$va['consignee_district'].$va['consignee_address'])
                    ->setCellValue('T'.$keys, $va['express_no'])
                    ->setCellValue('U'.$keys, implode(',',$package_status));

            }
            $objPHPExcel->setActiveSheetIndex(0);

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            header('pragma:public');
            header("Content-Disposition:attachment;filename=订单列表".date('_YmdHis').".xlsx");
            $objWriter->save('php://output');exit;
        }
    }

    public function actionDai(){
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $orderLogic = new OrderLogic();
        $this->data['breadcrumbs'] = '代采购订单管理';
        if(Yii::$app->request->post('submit')=="import"){
            if($_FILES['input_file']['name']){
                LocalUpload::go_upload_file($_FILES['input_file']);
                $uploadresult = '/uploads/'.LocalUpload::$filedir.'/'.LocalUpload::$filename;
                $objPHPExcel = new \PHPExcel();
                $objPHPExcel = \PHPExcel_IOFactory::load(dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'web'.$uploadresult);
                $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
                foreach($sheetData AS $key=>$value){
                    if($key==0)
                        continue;
                    if($value['A']!="用户名" && preg_match("/^1[34578]\d{9}$/", $value['A']))
                        if(isset($value['J'])){
                            $ordertemp[$value['A'].$value['B'].$value['C'].$value['D'].$value['J']][] = $value;
                        }else{
                            $ordertemp[$value['A'].$value['B'].$value['C'].$value['D']][] = $value;
                        }
                }
                foreach($ordertemp AS $k=>$v){
                    $orderLogic->submitOrder($v);
                }

            }else{
                $uploadresult = '';
            }
        }elseif(Yii::$app->request->get('submit')=="export"){

            $order = $orderLogic->getDaiOrder($query,1,null);
            //var_dump($order);exit;
            $objPHPExcel = new \PHPExcel();
            $objPHPExcel->getProperties()->setCreator("中经汇通")
                ->setTitle("代采购订单列表");
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '用户名')
                ->setCellValue('B1', '订单号')
                ->setCellValue('C1', '手机号')
                ->setCellValue('D1', '收货地址')
                ->setCellValue('E1', '收货人')
                ->setCellValue('F1', '实付金额')
                ->setCellValue('G1', '订单状态');

            $over_time = "";
            $status = "";
            foreach($order['lists'] AS $ke=>$va){
                switch($va['order_status']){
                    case 0:
                        $status = "中经待支付";
                        break;
                    case 1:
                        $status = "中经已付款";
                        break;
                }
                $keys = $ke+2;
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValueExplicit('A'.$keys, $va['mobile'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValueExplicit('B'.$keys, $va['order_no'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValueExplicit('C'.$keys, $va['consignee_phone'],PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue('D'.$keys, $va['consignee_province'].$va['consignee_city'].$va['consignee_district'].$va['consignee_address'])
                    ->setCellValue('E'.$keys, $va['consignee'])
                    ->setCellValue('F'.$keys, $va['order_amount'])
                    ->setCellValue('G'.$keys, $status);

            }
            $objPHPExcel->setActiveSheetIndex(0);

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            header('pragma:public');
            header("Content-Disposition:attachment;filename=代采购订单列表".date('_YmdHis').".xlsx");
            $objWriter->save('php://output');exit;

        }
        $order = $orderLogic->getDaiOrder($query, $page);
        return $this->render('dai', [
            'theme' => '/assets/',
            'order' => $order,
            'query' => $query
        ]);
    }


    public function actionOrderDetail()
    {
        $orderLogic = new OrderLogic();
        $id = Yii::$app->request->get('id');
        $order = $orderLogic->getById($id);
        $order_goods = $orderLogic->getOrderGoodsById($id);
        $order_goods_result = [];
        foreach ($order_goods AS $value){
            if(isset($value['package_id'])){
                $order_goods_result[$value['package_id']]['package_status'] = $value['package_status'];
                $order_goods_result[$value['package_id']]['list'][] = $value;
            }else{
                $order_goods_result[0]['package_status'] = $value['package_status'];
                $order_goods_result[0]['list'][] = $value;
            }

        }
        //var_dump($order_goods_result[0]['list']);exit;
        $package =  $orderLogic->getPackageById($id);
        $package_result = [];
        foreach ($package AS $value){
            $package_result[$value['package_id']][] = $value;
        }
        $package_result = array_values($package_result);
        $this->data['breadcrumbs'] = ['订单管理' => U('order/index')];

        return $this->render('detail', [
            'pic_domain'=>Yii::$app->params['pic_domain'],
            'order' => $order,
            'order_goods' => $order_goods_result,
            'package' => $package_result,
            'theme' => '/assets/'
        ]);
    }

    public function actionCustomerService()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $orderLogic = new OrderLogic();
        $this->data['breadcrumbs'] = '退货订单';
        $order = $orderLogic->getService($query, $page);
        if(Yii::$app->request->get('submit')!="export"){
            return $this->render('customer-service', [
                'theme' => '/assets/',
                'order' => $order,
                'query' => $query
            ]);
        }else{
            $order = $orderLogic->getService($query,1,null);
            // 创建Excel文件对象
            $objPHPExcel = new \PHPExcel();
            $objPHPExcel->getProperties()->setCreator("中经汇通")
                ->setTitle("退货列表");
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1', '申请单号')
                ->setCellValue('B1', '手机号')
                ->setCellValue('C1', '订单号')
                ->setCellValue('D1', '商品名称')
                ->setCellValue('E1', '商品数量')
                ->setCellValue('F1', '退款金额')
                ->setCellValue('G1', '退货状态')
                ->setCellValue('H1', '退货原因')
                ->setCellValue('I1', '申请时间')
                ->setCellValue('J1', '打款状态');
            $status = "";
            foreach($order['lists'] AS $ke=>$va){
                switch($va['yanxuan_status']){
                    case 1:
                        $status = "待审核";
                        break;
                    case 2:
                        $status = "待用户寄回";
                        break;
                    case 3:
                        $status = "用户已寄送";
                        break;
                    case 4:
                        $status = "退货成功";
                        break;
                    case 5:
                        $status = "客服审核不通过";
                        break;
                    case 6:
                        $status = "用户取消";
                        break;
                    case 7:
                        $status = "系统取消";
                        break;
                    case 8:
                        $status = "客服取消";
                        break;
                    case 9:
                        $status = "待客服确认";
                        break;
                    case 11:
                        $status = "客服拒绝";
                        break;
                }
                if($va['reason_type']==0){
                    $reason = "无理由";
                }else{
                    $reason = "质量问题";
                }
                if($va['is_refund']==0){
                    $is_refund = "未打款";
                }else{
                    $is_refund = "已打款";
                }
                $keys = $ke+2;
                $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.$keys, $va['service_no'])
                    ->setCellValue('B'.$keys, $va['mobile'])
                    ->setCellValue('C'.$keys, $va['order_no'])
                    ->setCellValue('D'.$keys, $va['goods_name'])
                    ->setCellValue('E'.$keys, $va['sku_num'])
                    ->setCellValue('F'.$keys, $va['service_amount'])
                    ->setCellValue('G'.$keys, $status)
                    ->setCellValue('H'.$keys, $reason)
                    ->setCellValue('I'.$keys, $va['service_time'])
                    ->setCellValue('J'.$keys, $is_refund);
            }
            $objPHPExcel->setActiveSheetIndex(0);

            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            header('pragma:public');
            header("Content-Disposition:attachment;filename=退货列表".date('_YmdHis').".xlsx");
            $objWriter->save('php://output');exit;
        }

    }

    public function actionServiceDetail()
    {
        $orderLogic = new OrderLogic();
        $id = Yii::$app->request->get('id');
        $order = $orderLogic->getServiceById($id);

        $this->data['breadcrumbs'] = ['订单管理' => U('order/index')];

        return $this->render('service-detail', [
            'pic_domain'=>Yii::$app->params['pic_domain'],
            'order' => $order,
            'theme' => '/assets/'
        ]);
    }

    public function actionRefund(){
        $orderLogic = new OrderLogic();
        $id = Yii::$app->request->get('id');
        $order = $orderLogic->refundById($id);

        Yii::$app->getSession()->setFlash('success','打款成功');
        jump('order/customer-service');
    }

}
